package by.grsu.samusev.page.pages;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;


import by.grsu.samusev.app.SamusevWebApplication;
import by.grsu.samusev.dao.impl.CarDao;
import by.grsu.samusev.entity.Car;

public class CarListPage extends WebPage {
	private final CarDao carDao;
		
	public CarListPage() {
		super();
		carDao = new CarDao(SamusevWebApplication.DATA_XML_FOLDER);
		someshit(carDao.getAll());
	}
	
	public CarListPage(List<Car> cars) {
		super();
		carDao = new CarDao(SamusevWebApplication.DATA_XML_FOLDER);
		someshit(cars);
	}
	
	private void someshit(final List<Car> cars) {
		add(new DataView<Car>("table-rows", new ListDataProvider<Car>(cars)) {
			@Override
			protected void populateItem(final Item<Car> item) {
				final Car car = item.getModelObject();

				item.add(new Label("id", car.getId()));
				item.add(new Label("status", car.getStatus()));
				item.add(new Label("characteristic", car.getCharacteristics()));
				item.add(new Link("editLink") {
					@Override
					public void onClick() {
						setResponsePage(new CarEditPage(car));
					}
				});

				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
						carDao.delete(car.getId());
						setResponsePage(new CarListPage());
					}
				});
			}
		});
	}
	
}
